package com.authine.cloudpivot.web.api.secondopen.service.impl;

import com.authine.cloudpivot.web.api.secondopen.dao.Ie6s6cbzxwhjcxxMapper;
import com.authine.cloudpivot.web.api.secondopen.service.BudgetImportService;
import com.authine.cloudpivot.web.api.secondopen.vo.BudgetVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class BudgetImportServiceImpl implements BudgetImportService {

    @Autowired
    Ie6s6cbzxwhjcxxMapper cbzxwhjcxxMapper;
    @Override
    public List<BudgetVO> queryBudget(String cbzx, String nf, String yf) {
        int intnf=Integer.parseInt(nf);
        int intyf=Integer.parseInt(yf);
        List<BudgetVO> budgetVOS = cbzxwhjcxxMapper.queryBudget(cbzx, intnf, intyf);
        List<BudgetVO> list =new ArrayList<>();
        for (int i = 0; i <budgetVOS.size() ; i++) {
            BudgetVO budgetVO = budgetVOS.get(i);
            BudgetVO params = getParams(budgetVO);
            list.add(params);
        }
        return list;
    }

    public BudgetVO getParams(BudgetVO budgetVO){
        BudgetVO params = new BudgetVO();
        //年份
        //String year = budgetVO.getYear();
        int year=Integer.parseInt(budgetVO.getYear());
        //月份
        //String yf = budgetVO.getYf();
        int yf=Integer.parseInt(budgetVO.getYf());
        //成本中心
        String cbzx = budgetVO.getCbzx();
        //一级科目
        String onesub = budgetVO.getOnesub();
        //二级科目
        String twosub = budgetVO.getTwosub();
        //根据年、月、成本中心、一级科目、二级科目查出月预算累计和月实绩累计
        BudgetVO vos = cbzxwhjcxxMapper.queryParams(year, yf, cbzx, onesub, twosub);
        //累计月预算
        BigDecimal ljyys = vos.getLjyys();
        //累计月实绩
        BigDecimal ljysj = vos.getLjysj();
        params.setId(budgetVO.getId());
        params.setYear(budgetVO.getYear());
        params.setCbzx(budgetVO.getCbzx());
        params.setOnesub(budgetVO.getOnesub());
        params.setTwosub(budgetVO.getTwosub());
        params.setYearlj(budgetVO.getYearlj());
        params.setYf(budgetVO.getYf());
        params.setDqyys(budgetVO.getDqyys());
        params.setDqysj(budgetVO.getDqysj());
        params.setLjyys(ljyys);
        params.setLjysj(ljysj);
        return params;
    }

    @Override
    public int insertBudget(List<BudgetVO> budgetVOList) {
        return  cbzxwhjcxxMapper.insertBudget(budgetVOList);
    }
}
